<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入检测站名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="联系电话" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入联系电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否启用" prop="isEnable">
        <el-select v-model="queryParams.isEnable" placeholder="是否启用" clearable size="small">
          <el-option v-for=" dict in enableOptions"
                     :key="dict.dictValue"
                     :label="dict.dictLabel"
                     :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="代驾启用" prop="isDjEnable">
        <el-select v-model="queryParams.isEnable" placeholder="代驾启用" clearable size="small">
          <el-option v-for=" dict in enableOptions"
                     :key="dict.dictValue"
                     :label="dict.dictLabel"
                     :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item label="自驾启用" prop="isZjEnable">
        <el-select v-model="queryParams.isEnable" placeholder="自驾启用" clearable size="small">
          <el-option v-for=" dict in enableOptions"
                     :key="dict.dictValue"
                     :label="dict.dictLabel"
                     :value="dict.dictValue"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="stationList" @selection-change="handleSelectionChange">
      <el-table-column type="expand">
        <template slot-scope="props">

          <el-card class="box-card">
            <div class="text item">
              <el-row :gutter="24" class="expand-item">
                <el-col :span="6">监测站位置：{{props.row.locPoi}}</el-col>
                <el-col :span="6">检测站坐标：{{props.row.coordinate}}</el-col>
                <el-col :span="6">行政区划：{{props.row.province}}-{{props.row.city}}-{{props.row.region}}</el-col>
                <el-col :span="6">营业日期：{{props.row.officeHours}}</el-col>
              </el-row>
              <el-row :gutter="24" class="expand-item">
                <el-col :span="6">上午：{{props.row.am}}</el-col>
                <el-col :span="6">下午：{{props.row.pm}}</el-col>
              </el-row>
            </div>
          </el-card>
        </template>
      </el-table-column>
      <el-table-column type="selection" width="55" align="center"/>

      <el-table-column label="检测站图片" align="center" prop="avatar">
        <template slot-scope="scope">
          <el-avatar shape="square" :size="80" :src="ossPreview+ scope.row.avatar"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column label="检测站名称" align="center" prop="name"/>
      <el-table-column label="联系电话" align="center" prop="mobile"/>
      <el-table-column label="是否启用" align="center" :formatter="isEnableFormat" prop="isEnable"/>
      <el-table-column label="代驾启用" align="center" :formatter="isDjEnableFormat" prop="isDjEnable"/>
      <el-table-column label="自驾启用" align="center" :formatter="isZjEnableFormat" prop="isZjEnable"/>
      <el-table-column label="排序" align="center" prop="sort"/>
      <el-table-column label="创建时间" align="center" prop="gmtTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.gmtTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleChoose(scope.row)"
            v-hasPermi="['driver:driver:chooseStation']"
          >分配
          </el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
  import {listStation} from "@/api/station/station";
  import {chooseStation} from "@/api/driver/driver";
  import defaultSettings from '@/settings';

  export default {
    name: "Station",
    props: {
      driverId: {
        type: Array
      }
    },
    data() {
      return {
        ossPreview: defaultSettings.ossPreview,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 检测站表格数据
        stationList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        priceopen: false,
        stationId:0,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: undefined,
          avatar: undefined,
          locPoi: undefined,
          coordinate: undefined,
          mobile: undefined,
          isEnable: undefined,
          isDjEnable: undefined,
          isZjEnable: undefined,
          stationFee: undefined,
          officeHours: undefined,
          am: undefined,
          pm: undefined,
          gmtTime: undefined
        },
        // 状态数据字典
        enableOptions: [],
        areaoptions: [],
        // 表单参数
        form: {
          name: '',
          avatar: '',
          locPoi: '',
          coordinate: '',
          lng: '',
          lat: '',
          mobile: '',
          isEnable: 1,
          isDjEnable: 1,
          isZjEnable: 1,
          officeHours: '',
          am: '',
          pm: '',
          stationFee: undefined,
          provinceCode:'',
          cityCode:'',
          regionCode:'',
          province:'',
          city:'',
          region:'',
          sort: 1
        },
        areaArr:[],
        // 表单校验
        rules: {
          name: [
            {request: true, message: '请输入检查站名称', trigger: 'blur'},
            {min: 1, max: 20, message: '长度在1到20个字符', trigger: 'blur'}
          ],
          mobile: [
            {validator: mobileValid, trigger: 'blur'}
          ]
        },
        option: {
          column: [
            {
              label: '检测站位置',
              labelWidth: '400px',
              labelPosition: 'right',
              prop: 'map',
              type: 'map',
              emptyBtn: true,
            }]
        },
        text: {
          formattedAddress: "郑州市人民政府",
          longitude: 113.625351,
          latitude: 34.746303
        },
        postForm: {
          detailedAddress: '',
          longitude: '',
          latitude: ''
        }
      };
      var mobileValid = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('联系方式不能为空'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入数字值'));
          } else {
            if (value.length > 20) {
              callback(new Error('长度在1到20个字符'));
            } else {
              callback();
            }
          }
        }, 1000);
      }
    },
    created() {
      this.getList();
      this.getDicts("sys_is_enable").then(response => {
        this.enableOptions = response.data;
      });
    },
    methods: {
      /** 查询检测站列表 */
      getList() {
        this.loading = true;
        listStation(this.queryParams).then(response => {
          this.stationList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          name: undefined,
          avatar: undefined,
          locPoi: "郑州市人民政府",
          coordinate: "113.625351,34.746303",
          mobile: undefined,
          isEnable: undefined,
          isDjEnable: undefined,
          isZjEnable: undefined,
          stationFee: undefined,
          gmtTime: undefined,
          provinceCode:'',
          cityCode:'',
          regionCode:'',
          province:'',
          city:'',
          region:'',
          sort: 1
        };
        this.resetForm("form");
        this.areaArr = [];
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length != 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加检测站";
      },
      /** 修改按钮操作 */
      handleChoose(row) {
        const stationId = row.id
        let _this = this;
        this.$confirm('是否确认分配给该检测站', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return chooseStation(_this.driverId,stationId);
        }).then(() => {
          this.$emit("emitChooseStation");
        }).catch(function () {
          console.log(JSON.stringify(_form))
        });

      },

      getPosition({address, lat, lng, name}) {
        this.postForm.detailedAddress = address;
        this.postForm.longitude = String(lng);
        this.postForm.latitude = String(lat);
      },
      handleAddressFun: function(value){
        console.log(value)
        var node = this.$refs['cascaderAddr'].getCheckedNodes()[0];
        var  thsAreaCode = node.pathLabels   //注意2： 获取label值
        console.log(thsAreaCode)
        this.form.provinceCode = value[0];
        this.form.cityCode = value[1];
        this.form.regionCode = value[2];
        this.form.province = thsAreaCode[0];
        this.form.city = thsAreaCode[1];
        this.form.region = thsAreaCode[2];
      },
      isEnableFormat(row, column) {
        return this.selectDictLabel(this.enableOptions, row.isEnable);
      },
      isDjEnableFormat(row, column) {
        return this.selectDictLabel(this.enableOptions, row.isDjEnable);
      },
      isZjEnableFormat(row, column) {
        return this.selectDictLabel(this.enableOptions, row.isZjEnable);
      }
    }

  };
</script>
<style>
  .expand-item {
    margin-top: 15px;
  }
</style>
